const errors = require('restify-errors');
// const rjwt = require('restify-jwt-community');
// const Customer = require('../models/Customer');
const config = require('../config');
const axios = require('axios').default;

const rootUrl = 'http://localhost:3000/';

async function request(url, opts) {
  url = `${rootUrl}${url}`;
  //console.log(url);
  // opts = Object.assign({
  //   timeout: [ '30s', '30s' ],
  //   dataType: 'json',
  // }, opts);
  //return this.ctx.curl(url, opts);
  return await axios({
    method: opts.method,
    url: url,
    data: opts.data,
    responseType: opts.contentType
  });
}

async function userRegister() {
  const params = {
    'username': "test12",
    'password': '123456'
  }
  const result = await request('register', {
    method: 'post',
    data: params,
    contentType: 'json',
  });

  //console.log(result);
  return result.data;
}

async function userLogin() {
  const params = {
    'username': "test12",
    'password': '123456'
  }
  const result = await request('auth', {
    method: 'post',
    data: params,
    contentType: 'json',
  });

  return result.data;
}


module.exports = server => {
  


  // Get Customers
  server.get('/test', 
    async (req, res, next) => {
      try {
        //const customers = await Customer.find({});
        //const result = await userRegister();
        const result = await userLogin();
        res.send(result);
        next();
      } catch (err) {
        return next(new errors.InvalidContentError(err));
      }
    }
  );
};